<template>
    <popup ref="popupRef" :title="popupTitle" :async="true" width="1000px" @confirm="handleSubmit" @close="handleClose">
        <el-form ref="formRef" :model="formData" label-width="105px" :rules="formRules">
            <el-form-item label="套餐单数" prop="name">
                <el-input v-model="formData.num" placeholder="请输入套餐单数" />
            </el-form-item>
            <el-form-item label="套餐价格" prop="name">
                <el-input v-model="formData.price" placeholder="请输入套餐价格" />
            </el-form-item>
        </el-form>
    </popup>
</template>

<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import {
    addpack,
    editpack,
    infopack
} from '@/api/setting/xjy'
import feedback from '@/utils/feedback'
let lists = reactive([]);
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑套餐' : '添加套餐'
})

const popupRef = shallowRef<InstanceType<typeof Popup>>()
const formRef = shallowRef<FormInstance>()
const emit = defineEmits(['success', 'close'])
const value = ref({

})
const id = ref(-1)
const handleSubmit = async () => {
    await formRef.value?.validate()
    const results = mode.value == 'edit' ? await editpack({ ...formData }) : await addpack(formData)
    value.value = results
    popupRef.value?.close()
    emit('success')
}
const route = useRoute()
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}
const handleClose = () => {
    emit('close')
}
const formLists = ref({

})
const formData = reactive({
    price: "",
    num: "",
    id: "",
})
const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}
const getDetail = async (row: Record<string, any>) => {
    id.value = row.id
    const data = await infopack({
        id: row.id
    })
    setFormData(data)
    mode.value = 'edit'
}
const getAdd = async (row: Record<string, any>) => {
    formData.chain_id = row
}
defineExpose({
    open,
    setFormData,
    getDetail,
    getAdd
})
</script>

<style scoped></style>
